package com.wom.player;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

import com.wom.blockfeatures.BlockUtils;
import com.wom.blocks.AbstractBlock;
import com.wom.blocks.AirBlock;
import com.wom.blocks.WaterBlock;
import com.wom.gui.Camera;
import com.wom.gui.KeyHandler;
import com.wom.worldmaker.GameProperties;
import com.wom.worldmaker.GameWorld;

public class ThePlayer extends AbstractCharacter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public double fallingSpeed=3.0;
	public double walkingSpeed=1.0;
	public boolean walking=false;
	public double xDirection=0;
	public boolean jumping=false;
	public boolean mousePressedLeft=false;
	public boolean mousePressedRight=false;


	public boolean inWater=false;
	public boolean falling=true;
	public double buoyancy=2.0;
	public double jumpAmount=fallingSpeed+1.0;
	public double jumpLimit=60;
	public long jumpTime=0;
	public Point eyeDir=new Point(0,0);



	public Rectangle r;
	public Camera cam;
	public int id[]={0,11};



	public ThePlayer(int x, int y, Color c, String name) {
		super(x, y, c, name);
		//setBounds((GameProperties.pixelSize.width/2)-(width/2),(GameProperties.pixelSize.height/2)-(height),width,height);



		cam=new Camera(0,0,GameProperties.windowSize.width,GameProperties.windowSize.height);
		//setBounds((cam.x+cam.width/2),cam.y+cam.height/2,GameProperties.blockSize,GameProperties.blockSize*2);

		//moveToGrid(GameWorld.gameBlocks.length/4,20);
		moveToGrid(20, 20);
		cam.x=this.x-(cam.width/2/GameProperties.pixelRatio);
		cam.y=this.y-(cam.height/2/GameProperties.pixelRatio)+this.height;

		//setLocation(cam.width/4-width,cam.height/4-height);
		r=new Rectangle();

	}

	@Override
	public
	void renderPlayer(Graphics g) {
		g.setColor(getColor());
		//g.drawRect(x-cam.x, y-cam.y, width,height);

		g.drawImage(GameWorld.tileTextures,
				(int)(x-cam.x), 
				(int)(y-cam.y), 
				(int)(x+width-cam.x),(int)(y+height-cam.y),
				id[0]*GameProperties.blockSize,
				id[1]*GameProperties.blockSize,
				id[0]*GameProperties.blockSize+(int)width+2,
				id[1]*GameProperties.blockSize+(int)height+2, null);

		//g.setColor(Color.RED);
		//g.drawRect(r.x-cam.x,r.y-cam.y,r.width,r.height);
		drawEyes(g);
	}

	private void drawEyes(Graphics g) {
		g.setColor(Color.BLACK);
		if(eyeDir.x==0 && eyeDir.y==0){

			g.fillRect((int)(x+6-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
			g.fillRect((int)(x+11-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
		}else if(eyeDir.x<0){
			g.fillRect((int)(x+4-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
			g.fillRect((int)(x+10-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
		}else if(eyeDir.x>0){
			g.fillRect((int)(x+7-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
			g.fillRect((int)(x+13-cam.x), 
					(int)(y+8-cam.y), 
					1,1);
		}else if(eyeDir.y>0){
			g.fillRect((int)(x+6-cam.x), 
					(int)(y+9-cam.y), 
					1,1);
			g.fillRect((int)(x+11-cam.x), 
					(int)(y+9-cam.y), 
					1,1);
		}else if(eyeDir.y<0){
			g.fillRect((int)(x+6-cam.x), 
					(int)(y+6-cam.y), 
					1,1);
			g.fillRect((int)(x+11-cam.x), 
					(int)(y+6-cam.y), 
					1,1);
		}

	}

	@Override
	public	void updatePlayer() {

		id[0]=0;
		id[1]=11;


		// set rolling eyes to normal
		eyeDir.setLocation(0,0);


		if(!collidedWithBlock(new Rectangle2D.Double(x,y+1,width,height+3))){
			if(inWater){

				y+=fallingSpeed/5;				cam.y+=fallingSpeed/5;
				falling=false;
				inWater=false;

			}else{
				// set rolling eyes to down
				eyeDir.setLocation(0,1);
				y+=fallingSpeed;
				cam.y+=fallingSpeed;
				falling=true;
			}

		} 

		if(walking){



			boolean cantMove=false;

			if(xDirection<0){
				// set rolling eyes to left
				eyeDir.setLocation(-1,eyeDir.y);
				cantMove=collidedWithBlock(new Rectangle2D.Double(x-1,y+1,width,height-2));
			}else if(xDirection>0){
				// set rolling eyes to Right
				eyeDir.setLocation(1,eyeDir.y);


				cantMove=collidedWithBlock(new Rectangle2D.Double(x,y+1,width+1,height-2));
			}

			if(!cantMove){

				if((x>=(GameWorld.gameBlocks.length*GameProperties.blockSize)-(GameProperties.blockSize) && xDirection>0)
						|| x<=0 && xDirection<0){


				}else{

					x+=xDirection;


					cam.x+=xDirection;


				}

			}

		}

		if(jumping){

			// set rolling eyes to up

			if(y>0){

				if(!collidedWithBlock(new Rectangle2D.Double(x+1,y-3,width-1,height/2))){
					eyeDir.setLocation(0,-1);
					if(inWater){


						y-=jumpAmount/4;
						cam.y-=jumpAmount/4;

						inWater=false;

					}else{
						y-=jumpAmount;
						cam.y-=jumpAmount;
					}


				}
			}

		}








		// check boundaries to stop the camera going outside the world coordinates

		// lower x bound
		//if(cam.x<=0 || x<(cam.width/GameProperties.pixelRatio/2)){
		//	cam.x=0;
		//	}

		//upper x bound
		//		if(this.x>=(((GameWorld.gameBlocks.length*GameProperties.blockSize)-cam.width/2/GameProperties.pixelRatio/2))){
		//					cam.x=((GameWorld.gameBlocks.length*GameProperties.blockSize)-(cam.width)+this.width);
		//		}


		// lower y bound
		//		if(cam.y<=0 || y<(cam.height/GameProperties.pixelRatio/2)){
		//			cam.y=0;
		//		}

		//upper y bound
		//		if(this.y>=((GameWorld.gameBlocks[0].length*GameProperties.blockSize)-cam.height/2)){
		//					cam.y=(GameWorld.gameBlocks[0].length*GameProperties.blockSize)-cam.height+this.height;
		//		}




		//r.setFrame(x-GameProperties.blockSize,	cam.y-2,width+GameProperties.blockSize*2,cam.height);

	}


	private boolean collidedWithBlock(Rectangle2D rect){


		// check if there is an air block beneath the player.
		AbstractBlock collider = null;



		for(int i=(int) (x/GameProperties.blockSize-1);i<(x+width)/GameProperties.blockSize+1;i++){
			for(int j=(int) (y/GameProperties.blockSize-1);j<(y)/GameProperties.blockSize+2;j++){

				if(i>=0 && i<GameWorld.gameBlocks.length && j>=0 && j<GameWorld.gameBlocks[0].length){

					collider= GameWorld.gameBlocks[i][j];


					if(collider instanceof AirBlock){
					}else if(collider instanceof WaterBlock){
									
						inWater=true;
					}
					else if(collider.intersects(rect)){
						return true;
					}
					
				}
			}
		}
	


	return false;
	


}






public void moveToGrid(int xp,int yp){
	x=xp*GameProperties.blockSize;
	y=yp*GameProperties.blockSize;
}




}







